package com.tentcoo.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.tentcoo.dao.SuAdminDao;
import com.tentcoo.entity.SuAdminEntity;

@Repository
public class SuAdminDaoImpl implements SuAdminDao{


	@Autowired
	private HibernateTemplate hibernateTemplate;

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addUser(SuAdminEntity admin) {
		// TODO Auto-generated method stub
		try{
		SuAdminEntity entity = new SuAdminEntity();
		entity.setUserName(admin.getUserName());
		entity.setPassWord(admin.getPassWord());
		hibernateTemplate.save(entity);
		}catch(Exception e){
			
			System.out.println(e.getMessage());
		}
		
	}

	@Override
	public void delUser(SuAdminEntity admin) {
		// TODO Auto-generated method stub
		try{	
		List<SuAdminEntity> list = new ArrayList<SuAdminEntity>();
		SuAdminEntity entity = new SuAdminEntity();
		entity.setUserName(admin.getUserName());
		entity.setPassWord(admin.getPassWord());
		list= hibernateTemplate.findByExample(entity);	
		 hibernateTemplate.delete(list.get(0));
		}catch(Exception e){			
			System.out.println(e.getMessage());
		}
	
	}

	@Override
	public void updateUser(SuAdminEntity admin) {
		// TODO Auto-generated method stub
		try{
			List<SuAdminEntity> list = new ArrayList<SuAdminEntity>();
			list= hibernateTemplate.findByExample(admin);
			list.get(0).setPassWord("33");
			list.get(0).setUserName("44");
			hibernateTemplate.update(list.get(0));
			
		}catch(Exception e){
			
			System.out.println(e.getMessage());

		}		
	}

	@Override
	public List<SuAdminEntity> findSuperManager(SuAdminEntity admin) {
		// TODO Auto-generated method stub
		try{
		List<SuAdminEntity> list = new ArrayList<SuAdminEntity>();
		SuAdminEntity entity = new SuAdminEntity();
		entity.setUserName(admin.getUserName());
		entity.setPassWord(admin.getPassWord());
		list= hibernateTemplate.findByExample(entity);	
		return list;
		
		}catch(Exception e){
			
			System.out.println(e.getMessage());
			return null;
		}
	} 

	
}
